# -*- coding: utf-8 -*-
"""
接收爬取到的数据，存文件
"""
class LaptopPipeline(object):

    def __init__(self, settings):
        self.settings = settings

    def process_item(self, item, spider):
        """
        item process
        """
        content = item['content']
        self.file.write(content+"\n")
        # 强制刷到磁盘
        self.file.flush()
        return item

    @classmethod
    def from_crawler(cls, crawler):
        return cls(crawler.settings)

    def open_spider(self, spider):
        print('open spider.')
        self.file = open('comment.txt', 'w+', encoding='utf-8')

    def close_spider(self, spider):
        print('close spider.')
        self.file.close()
